package com.wcity.manage.web.property;

import com.wcity.manage.common.context.SessionKey;
import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.model.Fix;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.property.PropertyFixService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 物业报修管理模块
 * Created by zm on 2016/12/12.
 */
@Controller
public class PropertyFixAct {

    @Resource private PropertyFixService propertyFixService;

    /**
     * 分页查询表单数据
     * @param fixInfo
     * @param pageModel
     * @param request
     * @Author zm
     */
    @RequestMapping("fix_list")
    public String  fixList (Fix fixInfo , PageModel pageModel, HttpServletRequest request) {
        int STARUS = 1;//未删除
        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        fixInfo.setUserId(user.getId());
        fixInfo.setRemark(STARUS);
        pageModel.setParams( fixInfo );
        request.setAttribute( "pageModel", pageModel );
        request.setAttribute( "fixInfo", fixInfo );
        request.setAttribute( "userId", user.getId() );
        request.setAttribute( "workNo", user.getWorkNo() );
        request.setAttribute( "list", propertyFixService.getPayTypeListPage( pageModel ) );

        return "propertyFix/fixList";
    }

    /**
     * 删除
     * @param request
     * @param response
     */
    @RequestMapping("fix_delect")
    public void fixDelect ( HttpServletRequest request,HttpServletResponse response ) {
        String id = request.getParameter("id");
        int STARUS = 0;
        String code = "200";
        String meg = "删除成功";
        try{
            int success = propertyFixService.fixDelect(id,STARUS);
            if( success == 0 ){
                code = "300";
                meg = "删除失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "删除失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "fix_list", "", "", ""));
    }

    /**
     * 跳转到处理页面
     * @param request
     * @return
     */
    @RequestMapping("to_fix_edit")
    public String toFixEdit ( HttpServletRequest request ) {
        String id = request.getParameter("id");
        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        request.setAttribute( "workNo", user.getWorkNo() );
        request.setAttribute( "list", propertyFixService.getFixInfo( id ) );
        return "propertyFix/fixEdit";
    }

    /**
     * 处理
     * @param fix
     * @param response
     */
    @RequestMapping("fix_edit")
    public void fixEdit (Fix fix, HttpServletResponse response ) {

        int STARUS = 1;
        fix.setRemark(STARUS);
        fix.setStatus("已处理");
        Fix info = propertyFixService.getFixInfo( fix.getId()+"" );
        if(info.getStatus().equals("已处理")){
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "已处理过，无需再次提交", "fix_list", "", "closeCurrent", "" ) );
            return;
        }
        String code = "200";
        String meg = "处理成功";
        try{
            int success = propertyFixService.fixEdit(fix);
            if( success == 0 ){
                code = "300";
                meg = "处理失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "处理失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "fix_list", "", "closeCurrent", ""));
    }
}
